package com.gitee.controller;

import com.gitee.pojo.Cart;
import com.gitee.sdk.CartSDK;
import com.gitee.sdk.CookbookSDK;
import com.gitee.service.ICartService;
import com.gitee.service.ICookbookFoodService;
import com.gitee.service.ICookbookService;
import com.gitee.vo.CookbookVo;
import com.gitee.vo.ResponseVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/client")
public class ClientController {
    @Autowired
    private ICookbookService cookbookService;

    @Autowired
    private ICookbookFoodService cookbookFoodService;

    @Autowired
    private ICartService cartService;

    @GetMapping("/cookbook/today")
    public CookbookSDK today(Integer cookbookType) {
        CookbookVo cookbookVo = cookbookService.getTodayCookbook(cookbookType);
        CookbookSDK cookbookSDK = new CookbookSDK();
        BeanUtils.copyProperties(cookbookVo, cookbookSDK);
        return cookbookSDK;
    }

    @GetMapping("/cart")
    public List<CartSDK> listCarts(Integer userId, Integer cookbookId, Integer cookbookType) {
        List<Cart> carts = cartService.listCarts(userId, cookbookId, cookbookType);
        List<CartSDK> cartSDKS = carts.stream().map(cart -> new CartSDK(cart.getFoodId(), cart.getQuantity(), cart.getSelected())).collect(Collectors.toList());
        return cartSDKS;
    }

    @DeleteMapping("/cart")
    public void removeCart(Integer userId, Integer foodId, Integer cookbookId, Integer cookbookType) {
        cartService.removeCart(userId, foodId, cookbookId, cookbookType);
    }

    @PutMapping("/cookbook/stock")
    void updateByCookbookIdAndFoodId(Integer cookbookId, Integer foodId, Integer stock) {
        cookbookFoodService.updateByCookbookIdAndFoodId(cookbookId, foodId, stock);
    }
}
